package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.tanhua.UserHolder;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.api.mongo.FriendApi;
import com.tanhua.db.UserInfo;
import com.tanhua.mongo.Comment;
import com.tanhua.mongo.Friend;
import com.tanhua.template.HuanXinTemplate;
import com.tanhua.vo.ContactVo;
import com.tanhua.vo.PageBeanVo;
import com.tanhua.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessagesService {

    @Reference
    private FriendApi friendApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity addFriends(Integer friendId) {

        // 探花互为好友
        friendApi.addFriends(UserHolder.getUser().getId(),friendId);

        // 环信互为好友
        huanXinTemplate.addContacts(UserHolder.getUser().getId(),friendId.longValue());

        return ResponseEntity.ok(null);
    }

    public ResponseEntity findFriends(Integer page, Integer pagesize) {

        // 分页查询好友的id
        PageBeanVo pageBean=friendApi.findFriends(page,pagesize,UserHolder.getUser().getId());
        List<Friend> items = (List<Friend>) pageBean.getItems();

        List<ContactVo> voList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            //带着好友的id查询好友的信息
            for (Friend friend : items) {
                UserInfo userInfo = userInfoApi.findById(friend.getFriendId());
                ContactVo vo = new ContactVo();
                BeanUtils.copyProperties(userInfo,vo);
                vo.setUserId(userInfo.getId().toString());
                vo.setCity(userInfo.getCity().split("-")[0]);

                voList.add(vo);
            }
        }
        //组装返回
        pageBean.setItems(voList);
        return ResponseEntity.ok(pageBean);
    }

    public ResponseEntity findPage(Integer page, Integer pagesize, int commentType) {

        PageBeanVo pageBean=commentApi.findPage(page,pagesize,commentType,UserHolder.getUser().getId());
        List<Comment> items = (List<Comment>) pageBean.getItems();

        ArrayList<UserCommentVo> userCommentList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            //去查userInfo表获取到点赞、评论、喜欢用户的详情
            for (Comment comment : items) {
                UserInfo userInfo = userInfoApi.findById(comment.getUserId());
                //封装
                UserCommentVo vo = new UserCommentVo();
                BeanUtils.copyProperties(userInfo,vo);
                vo.setCreateDate(DateUtil.format(new Date(comment.getCreated()),"yyyy-MM-dd HH:mm:ss"));
                userCommentList.add(vo);
            }
        }

        pageBean.setItems(userCommentList);
        return ResponseEntity.ok(pageBean);
    }
}
